Home Java Docker
Home     Java

Java Topic

What is Java
History of Java
Freature of Java
Difference Between Java & C++
Java Environment Set Up
Java Hello World Program & its Internal Process
Java Hello World Program
JDK, JRE and JVM
Java Variables
Java Data Types & Unicode System
Java Operators
Java Keywords
Java Control Statements
Java if else
Java switch
Java for loop
Java While loop
Java Do While loop
Java break
Java continue
Java Oops Concept
Java Object & Class
Java Method
Java Constructor
Java Static Keyword
Java this Keyword
Java Inheritance
Java Hybrid Inheritance
Aggregation(HAS-A)
Java Polymorphism
Java method overloading
Java method overriding
Java Runtime polymorphism
Java Dynamic Binding
Super keyword
Final keyword
Difference Between method overloading and method overriding
Java Abstraction
Java Interface
Abstract class vs Interface
Java Encapsulation
Java Package
Java Access Modifiers
covariant return type
Instance initializer block
Java instanceof operator
Object Cloning in Java
Wrapper classes in Java
Java Strictfp Keyword
Recursion in Java
Java Command Line Arguments
Difference between object and class
Java String
Java String Class
Java Immutable String
Java Immutable Class
String Buffer
String Builder
String Buffer vs String
String Builder vs String Buffer
String Tokenizer in Java
Java Array
Java Exceptions Handling
Java Try-Catch block
Java Multiply Catch Block
Java Finally Block
Java Throws Keyword
Java Throw Keyword
Java Exception Propagation
Java Throw vs Throws
Final vs Finally vs Finalize
Exception Handling With Method Overridding
Java Multithreading
Lifecycle and States of a Thread in Java
How to create a thread in Java
Thread Scheduler in Java
Sleeping a thread in Java
Calling run() method
Joining a thread in Java
Naming a thread in Java
Thread Priority
Daemon Thread
Thread Pool
Thread Group
Shutdown hook
Multitasking vs Multithreading
Garbage Collection
RunTime Class
Java Synchronization
Synchronized block in Java
Static Synchronization in Java
Deadlock in Java
Inter Thread Communication in Java
Interrupting Thread in Java
Reentrant Monitor in Java
Java Applet
Animation in Applet
EventHandling in Applet
Display image in Applet
Displaying Graphics in Applet
Parameter in Applet
Java 8 Features
Java Lambda Expressions
Method References
Functional Interfaces
Java 8 Stream
Base64 Encode Decode
Default Method
for Each() Method
Collectors class
String Joiner Class
Optional Class
JavaScript Nashron
Parallel Array Sort
Type Interface
Parameter Reflection
Type and Repeating Annotations
JDBC Improvements

Polymorphism in Java

    Polymorphism refers to an object ability to have on multiple forms. Polymorphism in Java refers to a class's ability to provide different implementations of a method depending on the type of object passed to the method.
    In simple word polymorphism in Java allows us to perform the same action in a different ways. Polymorphism in Java refers to any Java object that can pass more than one IS-A test. As a result, all Java objects are polymorphic because they passed the IS-A test for their own type and the class Object.

Table Of Content

  • What is Polymorphism in Java
  • What is Method Overloading
  • What is Method Overriding
  • What is Compile time Polymorphism
  • What is Runtime Polymorphism in Java?






Polymorphism in real world sanario

  • A person can have multiple characteristics at the same time. A man can be a father, a husband, and an employee all at the same time. As a result, the same person behaves differently in different situations. This is what known as polymorphism. Polymorphism is regarded as an important piller of Object-Oriented Programming.
  • Polymorphism allows us to carry out a single action in multiple ways. Polymorphism, in other words, allows you to define one interface and have multiple implementations. The word "poly" means "many," and "morphs" means "forms," so it means multiple forms.

Java polymorphism example
 // Java polymorphism example  
class GrandParent {
    void print() {
    System.out.println("Hi I am GrandFather");
  }
}
class Parent extends GrandParent{
  void print() {
    System.out.println("Hi I am Father");
  }
}
class Child extends Parent {
  void print() {
    System.out.println("Hi I am Child");
  }
}
public class Main {
  void declaration(String name) {
    System.out.println("Hi myself " + name);
  }
  void declaration(String fname, String lname) {
    System.out.println("Hi myself " + fname + " " + lname);
  }
  public static void main(String[] args) {
      // GrandParent class instance
	  System.out.println("---------------Run-time polymorphism---------------");
	  GrandParent grandParent ;
	  grandParent = new GrandParent();
	  grandParent.print();
	  grandParent = new Parent();
	  grandParent.print();
	  grandParent = new Child();
	  grandParent.print();
	    
     System.out.println("---------------Complile time ploymorphism---------------");
     Main main = new Main();
     main.declaration("Alok.");
     main.declaration("Alok", "Raja.");
  }
}




Output:

---------------Run-time polymorphism--------------- 
Hi I am GrandFather 
Hi I am Father 
Hi I am Child 
---------------Complile time ploymorphism--------------- 
Hi myself Alok. 
Hi myself Alok Raja. 

Types of Polymorphism in java

  • In Java, there are 2 different types of Polymorphism that can be used:
    • Compile time Polymorphism
    • Runtime Polymorphism
  • To perform Polymorphism in Java we have two different methods:
    • Method Overloading
    • Method Overriding

1.What is Method Overloading


Method overloading is the process of creating multiple methods with the same name in the same class, each of which works differently. Method overloading is a situation when a class contains multiple methods with the same name but having different papameter and return type.


Java program with Method Overloading
 // Java program with method overloading  
class Product {
    int Multiply(int p, int q){  
     return p * q;
  }

  float Multiply(float p, float q){   
	  return p * q;
  }
}
class Overloading {
 public static void main(String[] args){
    Product product=new Product();
     System.out.println(product.Multiply(2, 4));
     System.out.println(product.Multiply(5.2f, 6.3f));
 }
}



Output:

8 
32.76 

Learn more about method overloading Next »



2.What is Method Overriding


Method overriding occurs when a subclass or child class declares the same method as the parent class.


Java program with Method Overriding
 // Java program with Method Overriding 
class Vehicle{  
    void run(){
	 System.out.println("Vehicle is moving");
   }  
}  	
class Bike extends Vehicle{  
  void run(){
	 System.out.println("Bike is running safely");
  }    
  public static void main(String args[]){  
    Bike obj = new Bike();
      obj.run();
  }  
} 



Output:

Bike is running safely 

Learn more about method overriding Next »



What is Compile time Polymorphism


Compile Time Polymorphism Static Polymorphism is another name for it in Java. Furthermore, the method call is resolved at compile time. Method Overloading is used to achieve compile-time polymorphism. Operator Overloading can also be used to achieve this type of polymorphism. However Java does not support Operator Overloading.


Method overloading does when a class has multiple methods with the same name, but the number, types, and order of parameters, as well as the return type, different. Java allows users to use the same name for multiple functions as long as they can be distinguished by the type and number of parameters.



Java program using Compile time Polymorphism
 // Java program  using Compile time Polymorphism 
class Product {
    void Multiply(int p, int q){  
    System.out.println("Addition of two numbers : " +(p+q)); 
  }
  void Multiply(int p, int q,int r){   
  	System.out.println("Addition of three numbers : " +(p+q+r)); 
  }
}
class Overloading {
 public static void main(String[] args){
	 Product product=new Product();
     product.Multiply(2, 4);
     product.Multiply(10, 20,30);
 }
}



Output:

Addition of two numbers : 6 
Addition of three numbers : 60 

The sum() method in this programme is overloaded with two types via different parameters.This is the baisc idea behind compile-time polymorphism in Java, where we can perform different operations by calling multiple methods with the same name.


What is Runtime Polymorphism in Java?

  • Java's runtime polymorphism is also referred to as dynamic binding or dynamic method dispatch. Java's runtime polymorphism resolves the call to an overridden method dynamically during runtime as opposed to compile-time. Runtime polymorphism can be achived through Method Overriding.
  • Method Overriding occurs when a child or subclass has a method with the same name, inputs, and output as the parent or superclass, that method overrides the superclass method. This is known as overriding. In simple terms, a method in the base class is said to be overridden if the subclass gives its definition to a method already present in the superclass.
  • It should be noted that data members cannot be used to achieve runtime polymorphism; only functions can.
  • Utilizing a superclass reference variable is how overriding is accomplished. The object that the reference variable is referring to determines which method needs to be called. Upcasting is another name for this. 
Learn more about runtime polymorphism Next »





Java program Runtime Polymorphism
 // Java program Runtime Polymorphism 
class car 
{ 
    int speedlimit = 25; 
    void run() { 
    System.out.println("Runnning at 25 kmph speed"); 
    } 
} 
class Nano extends car 
{ 
    int speedlimit = 35; 
    void run() { 
    	  System.out.println("Runnning at 35 kmph speed"); 
    } 
    public static void main(String args[]) 
    { 
    car obj = new Nano(); 
    System.out.println(obj.speedlimit);
    obj.run();
    }
}



Output:

25   // run time polymorphism achive 
Runnning at 35 kmph speed 


Advantages of Polymorphism in Java

  • Polymorphism allows the code to be reused. Classes that have been written, tested, and implemented can be reused several times. Furthermore, it saves the coder a significant amount of time. Furthermore, the code can be changed without affecting the original code.
  • Multiple data values can be stored in a single variable. The value of a variable inherited from the superclass into the subclass can be altered without altering the value of that variable in the superclass or any other subclass.
  • Debugging the code gets easier for the coder with fewer lines of code.
Java method overloading Next »
« Perv Next »


Post your comment





Read Next Topic
Java Tutorial - Topic
Java Polymorphism
Java method overloading
Java method overriding
Java Runtime polymorphism
Java Dynamic Binding
Super keyword
Final keyword
Difference Between method overloading and method overriding
Difference between Early and Late Binding in Java
Read Other Java Chapter
Java Topic
Java Basic Tutorial
Java Control Statements
Java Classes & Object
Java Inheritance
Java Polymorphism
Java Abstraction
Java Encapsulation
Java OOPs Miscellaneous
Java Array
Java String
Java Exception Handling
Java Multithreading
Java Synchronization
Java Applet
Java 8 Features
Java 9 Features
Java Collection
Java Mcq
Java Interview Question
Tools
  

Useful Links

  • Home
  • Blog
  • About us
  • Contact Us
  • Privacy policy

Contact Us

Police Colony
Patna, Bihar
India

Email:

About DockerTpoint


India's largest site for Programming Tutorial as well as BANK, SSC, RAILWAY exam
and Campus placement preparation.